package cn.cs.controller;


import cn.cs.common.ApiReturnObject;
import cn.cs.common.ApiReturnUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

@Controller
@RequestMapping("/checkcenter")
public class CheckCenterController {

    //页面请求
    @GetMapping("/socket/{cid}")
    public ModelAndView socket(@PathVariable String cid) {
        ModelAndView mav=new ModelAndView("/socket");
        mav.addObject("cid", cid);
        return mav;
    }
    //推送数据接口
    @ResponseBody
    @RequestMapping("/socket/push/{cid}")
    public ApiReturnObject pushToWeb(@PathVariable String cid, String message) {
        try {
            WebSocketServer.sendInfo(message,cid);
        } catch (IOException e) {
            e.printStackTrace();
            return ApiReturnUtil.error(cid+"#"+e.getMessage());
        }
        return ApiReturnUtil.success(cid);
    }
    //推送数据接口
    @Scheduled(cron = "*/5 * *  * * ?")
    @ResponseBody
    @RequestMapping("/socket/push/20")
    public ApiReturnObject pushToWeb1() {
        try {
            WebSocketServer.sendInfo("hello websocket","20");
        } catch (IOException e) {
            e.printStackTrace();
            return ApiReturnUtil.error("20"+"#"+e.getMessage());
        }
        return ApiReturnUtil.success("20");
    }
}
